package com.quyunshuo.module.home.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View

@Deprecated(message = "练习用的")
class MyClircleProgressView(
    context: Context,
    attrs: AttributeSet? = null,
) : View(context, attrs) {

    /*
        1. 先创建画笔
     */
    private val bgPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.STROKE  //描边模式。只绘制图形的轮廓（边框），不填充内部。

    }
    private val progressPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.STROKE  //描边模式。只绘制图形的轮廓（边框），不填充内部。
        strokeCap = Paint.Cap.ROUND // 进度条两端为圆角
    }

    // 默认颜色值
    private var defaultBackgroundColor = Color.LTGRAY
    private var defaultProgressColor = Color.BLUE
    /*
        2. 初始化，画笔颜色等等。
     */
    init{
        bgPaint.strokeWidth = 15f
        bgPaint.color = defaultBackgroundColor

        progressPaint.strokeWidth = 15f
        progressPaint.color = defaultProgressColor
    }

    /*
        3.绘制在那个位置呢？？？
     */
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        // 确保视图为正方形（取宽高中较小者）
        val width = MeasureSpec.getSize(widthMeasureSpec)
        val height = MeasureSpec.getSize(heightMeasureSpec)
        val size = width.coerceAtMost(height)  // 取宽度和高度的最小值

        setMeasuredDimension(size,size)
    }

    // 4. 绘制区域矩形
    private val oval = RectF()

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        // 计算绘制的矩形区域，需要考虑线条宽度的影响
        val padding = 15f / 2  // 内边距（确保完整显示线条）

        oval.set(
            padding,
            padding,
            w-padding,
            h-padding

        )
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        canvas.drawOval(oval,bgPaint)
        canvas.drawArc(oval,-90f,(30/100f)*360f,false,progressPaint)

    }
}